﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;

namespace WFL.Threading
{
    /// <summary>
    /// 定时任务基类（定时任务如果手动运行实例，不会影响其定时循环执行的机制）
    /// </summary>
    public abstract class TimingBatch : Batch
    {
        /// <summary>
        /// 定时任务开始时间
        /// </summary>
        public DateTime StartTime
        {
            get;
            internal set;
        }

        /// <summary>
        /// 定时任务执行间隔
        /// </summary>
        public TimeSpan Interval
        {
            get;
            internal set;
        }

        /// <summary>
        /// 定时任务下次到期时间
        /// </summary>
        internal DateTime NextDueTime
        {
            get;
            set;
        }

        public TimingBatch(string name) : base(name)
        {

        }

        public TimingBatch(string name, NameValueCollection parameters) : base(name, parameters)
        {
            if (parameters != null)
            {
                string paramString = parameters["startTime"];
                if (!string.IsNullOrEmpty(paramString))
                {
                    StartTime = DateTime.Parse(paramString);
                }
                paramString = parameters["interval"];
                if (!string.IsNullOrEmpty(paramString))
                {
                    Interval = TimeSpan.Parse(paramString);
                }
            }
        }

        public TimingBatch(string name, DateTime startTime, TimeSpan interval) : base(name)
        {
            StartTime = startTime;
            Interval = interval;
        }
    }
}
